using Coldairarrow.Util;
using Coldairarrow.Util.Others;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace Coldairarrow.Business.SubApp
{
    public class RemoteRoleBusiness : ITransientDependency
    {
        private readonly PostRequestHelper _postRequestHelper;
        private ILogger<RemoteRoleBusiness> _logger;
        public RemoteRoleBusiness(PostRequestHelper postRequestHelper,
          ILogger<RemoteRoleBusiness> logger)
        {
            _postRequestHelper = postRequestHelper;
            _logger = logger;
        }

        /// <summary>
        /// 同步上传角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public async Task<Resource> CreateRole(CIAMRole role)
        {

            PostInputDTO input = new PostInputDTO();
            input.BaseAddress = RemoteApi.BaseUrl;
            input.APISite = RemoteApi.RoleCreateUrl;
            input.JsonData = JsonSerializer.Serialize(role);
            string result = await _postRequestHelper.GetPostData(input);
            var ajaxResult = JsonSerializer.Deserialize<AjaxResult<Resource>>(result);
            if (ajaxResult.Success)
            {
                _logger.LogDebug("同步上传角色成功");
                return ajaxResult.Data;
            }
            else
            {
                string msg = "同步上传角色失败:" + ajaxResult.Msg;
                _logger.LogDebug(msg);
                return null;
            }
           
        }


        /// <summary>
        /// 同步上传角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public async Task UpdateRole(CIAMRole role)
        {
          
            PostInputDTO input = new PostInputDTO();
            input.BaseAddress = RemoteApi.BaseUrl;
            input.APISite = RemoteApi.RoleUpdateUrl;
            input.JsonData = JsonSerializer.Serialize(role);
            string result = await _postRequestHelper.GetPostData(input);
            var ajaxResult = JsonSerializer.Deserialize<AjaxResult<Resource>>(result);
            if (ajaxResult.Success)
            {
                _logger.LogDebug("同步更新角色成功");
                //return ajaxResult.Data;
            }
            else
            {
                string msg = "同步更新角色失败:" + ajaxResult.Msg;
                _logger.LogDebug(msg);
                //return null;
            }
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteRole(string id)
        {
            PostInputDTO input = new PostInputDTO();
            input.BaseAddress = RemoteApi.BaseUrl;
            input.APISite = RemoteApi.RoleDeleteUrl;
            List<string> ids = new List<string>() { id };
            input.JsonData = JsonSerializer.Serialize(ids);
            string result = await _postRequestHelper.GetPostData(input);
            var ajaxResult = JsonSerializer.Deserialize<AjaxResult<Resource>>(result);
            if (ajaxResult.Success)
            {
                _logger.LogDebug("同步删除角色成功");
                //return ajaxResult.Data;
            }
            else
            {
                string msg = "同步删除角色失败:" + ajaxResult.Msg;
                _logger.LogDebug(msg);
                //return null;
            }
        }
    }

    public class CIAMRole : Resource
    {
        /// <summary>
        /// 角色名
        /// </summary>
        public String RoleName { get; set; }

    }

    public class Resource
    {
        public string Id { get; set; }
        
        public string Token { get; set; }
    }
}
